import requests
import execjs
import re
import os

cookies = {
    'NMTID': '00OYfh04JTLlKqT-E-YrfyhoEysMn8AAAGVz_BaNw',
    '_ntes_nnid': 'ebf53fea2e69a3aa391ab9f1c2313532,1742950390796',
    '_ntes_nuid': 'ebf53fea2e69a3aa391ab9f1c2313532',
    'WEVNSM': '1.0.0',
    'WNMCID': 'rjrzws.1742950391167.01.0',
    'WM_TID': 'qgY4KEpxWSZAAQFFBFeTN%2FH%2B%2B1V27W0G',
    'sDeviceId': 'YD-BWYxF95%2BJWFBEgQAAUbWc%2BGvrhEsjhdi',
    'ntes_utid': 'tid._.z2joKF85aExFAlFEQVPTcvHq%252FkQ8mbSx._.0',
    '__snaker__id': 'TzDSkuo6r3Qdk2ZS',
    '__remember_me': 'true',
    'ntes_kaola_ad': '1',
    'gdxidpyhxdE': 'lJ5MxUd2e2GiQIRWXxPOiSp5z4y290iad80o%2Fbhen%2Bzm%2Bd6Y6BhLn1EZ9cJ9nrmBDX2mnOUCOH91z%5CEP%2Fl%5Cj%2F8ma%2FlUvBZ2BDAx9XLdQOOados3ie8Ho7osIY9Hbd9DXuugrTmdXYvw6%2F6nNcH1gQ9HHKLZ1KKCExP5ifPQldIU85PIs%3A1744163669763',
    'P_INFO': '13435353947|1744163050|1|music|00&99|null&null&null#gud&440300#10#0|&0|null|13435353947',
    '__csrf': 'fa390fc86346dc1abaf9b75e428aa241',
    'MUSIC_U': '00E7C8B9FCD6935A6A1DC7CDC8255ABA150F5309BEBA2F5C7617C554C1B989040EE0E575FF04604CE9322E902B9F57DF9135941A8BDF4CC9559E5EA6BE813D96798CC8677B822A7D39EFB84A595E0BB665A6291C8E23601323B99B421FB1894DB856C408A242240E87D143A099F002AA5EBB4D446186C9A32EDC43EDA9DEAE09E085E29EF21561832BE5E626C4B660A97C696FF27D86DABBAD44926134BE43C17EF6BAF0012FCA450CFF4DC6FC88BAE4CFC9B5AA3E79EB263EE5476D85449C4BF47E92A6CC2AC197BC9424C0A04B96BEEF6AB8ED4A7634189ADF5C14F645865A5DBDF1F51675D17A494659DD5283460BD75F32C39B1B5D61A4AE0037B395F6B73D8E251D9442EAA46887A156B2AB9866E17D74C0669515A4B1B6B578D857789B25095A3D186CDA4D6B248A757E841B51775F337BF00410A6EF1E9A11148D89731B6DD25A41A044DE017B89989D297F7EA7303797EB56CFDE57F5A5EF0A6734EFC7',
    'WM_NI': 'hACd0jwrOM%2BgOdC8DZOzKvlEBG9fVR9TF4RrI4IXkwTXVjTuqNLrptkDx5bvsRZpErQIPkWnxTbbbIIrLq69FA9Xzc5dyLXPEq3Li6MLCqbvwFA5HV3dky%2B5wUr3zIbJQXQ%3D',
    'WM_NIKE': '9ca17ae2e6ffcda170e2e6ee95b45dbcb7ffa2cb64f5ac8ab2c45a969f9f83c76f87a68ca8b77fbc8b8fadcf2af0fea7c3b92aac9f8d85b362b5ae81aeaa6d8dbc8dd9c7528da68597ef4f8299f787d55afb8c8ad5d17a8bec87b9ea7d98889a8bd77fb3928493f380a99cb8d6e17eb18e9691c87a9397a08cd666bc8cae83e1438bae8492b533869ff7b9f969f7bcb7addb5c87affdadec3e8f948d92e17eb59a8fd6cb5ef1bca59aaa3fb58bbed4f943a2969fb9d437e2a3',
    'JSESSIONID-WYYY': 'rJ3WxTi8UZiRH1l%2FzPANd%2FHg%2FC9C7CQKICnN7a3mR6DMtS992%2BnYMGRlFPZ83xivoY7esqI8zF0%5CryK6p%2Fn4ZmbaSYzzi1%2Bh3Wof%5C3O8G4r5xgNuaWtirhTmACT2jdv1zQq1vw1SF%2FxOIci9yqdNGgMk1uPahjmbCVlREnHtwc%2Bbe%5C86%3A1744462503374',
    '_iuqxldmzr_': '33',
    'playerid': '54912904',
}
headers = {
    'accept': '*/*',
    'accept-language': 'zh-CN,zh;q=0.9,en;q=0.8',
    'content-type': 'application/x-www-form-urlencoded',
    'origin': 'https://music.163.com',
    'priority': 'u=1, i',
    'referer': 'https://music.163.com/',
    'sec-ch-ua': '"Google Chrome";v="135", "Not-A.Brand";v="8", "Chromium";v="135"',
    'sec-ch-ua-mobile': '?0',
    'sec-ch-ua-platform': '"Windows"',
    'sec-fetch-dest': 'empty',
    'sec-fetch-mode': 'cors',
    'sec-fetch-site': 'same-origin',
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36',
    # 'cookie': 'NMTID=00OYfh04JTLlKqT-E-YrfyhoEysMn8AAAGVz_BaNw; _ntes_nnid=ebf53fea2e69a3aa391ab9f1c2313532,1742950390796; _ntes_nuid=ebf53fea2e69a3aa391ab9f1c2313532; WEVNSM=1.0.0; WNMCID=rjrzws.1742950391167.01.0; WM_TID=qgY4KEpxWSZAAQFFBFeTN%2FH%2B%2B1V27W0G; sDeviceId=YD-BWYxF95%2BJWFBEgQAAUbWc%2BGvrhEsjhdi; ntes_utid=tid._.z2joKF85aExFAlFEQVPTcvHq%252FkQ8mbSx._.0; __snaker__id=TzDSkuo6r3Qdk2ZS; __remember_me=true; ntes_kaola_ad=1; gdxidpyhxdE=lJ5MxUd2e2GiQIRWXxPOiSp5z4y290iad80o%2Fbhen%2Bzm%2Bd6Y6BhLn1EZ9cJ9nrmBDX2mnOUCOH91z%5CEP%2Fl%5Cj%2F8ma%2FlUvBZ2BDAx9XLdQOOados3ie8Ho7osIY9Hbd9DXuugrTmdXYvw6%2F6nNcH1gQ9HHKLZ1KKCExP5ifPQldIU85PIs%3A1744163669763; P_INFO=13435353947|1744163050|1|music|00&99|null&null&null#gud&440300#10#0|&0|null|13435353947; __csrf=fa390fc86346dc1abaf9b75e428aa241; MUSIC_U=00E7C8B9FCD6935A6A1DC7CDC8255ABA150F5309BEBA2F5C7617C554C1B989040EE0E575FF04604CE9322E902B9F57DF9135941A8BDF4CC9559E5EA6BE813D96798CC8677B822A7D39EFB84A595E0BB665A6291C8E23601323B99B421FB1894DB856C408A242240E87D143A099F002AA5EBB4D446186C9A32EDC43EDA9DEAE09E085E29EF21561832BE5E626C4B660A97C696FF27D86DABBAD44926134BE43C17EF6BAF0012FCA450CFF4DC6FC88BAE4CFC9B5AA3E79EB263EE5476D85449C4BF47E92A6CC2AC197BC9424C0A04B96BEEF6AB8ED4A7634189ADF5C14F645865A5DBDF1F51675D17A494659DD5283460BD75F32C39B1B5D61A4AE0037B395F6B73D8E251D9442EAA46887A156B2AB9866E17D74C0669515A4B1B6B578D857789B25095A3D186CDA4D6B248A757E841B51775F337BF00410A6EF1E9A11148D89731B6DD25A41A044DE017B89989D297F7EA7303797EB56CFDE57F5A5EF0A6734EFC7; WM_NI=hACd0jwrOM%2BgOdC8DZOzKvlEBG9fVR9TF4RrI4IXkwTXVjTuqNLrptkDx5bvsRZpErQIPkWnxTbbbIIrLq69FA9Xzc5dyLXPEq3Li6MLCqbvwFA5HV3dky%2B5wUr3zIbJQXQ%3D; WM_NIKE=9ca17ae2e6ffcda170e2e6ee95b45dbcb7ffa2cb64f5ac8ab2c45a969f9f83c76f87a68ca8b77fbc8b8fadcf2af0fea7c3b92aac9f8d85b362b5ae81aeaa6d8dbc8dd9c7528da68597ef4f8299f787d55afb8c8ad5d17a8bec87b9ea7d98889a8bd77fb3928493f380a99cb8d6e17eb18e9691c87a9397a08cd666bc8cae83e1438bae8492b533869ff7b9f969f7bcb7addb5c87affdadec3e8f948d92e17eb59a8fd6cb5ef1bca59aaa3fb58bbed4f943a2969fb9d437e2a3; JSESSIONID-WYYY=rJ3WxTi8UZiRH1l%2FzPANd%2FHg%2FC9C7CQKICnN7a3mR6DMtS992%2BnYMGRlFPZ83xivoY7esqI8zF0%5CryK6p%2Fn4ZmbaSYzzi1%2Bh3Wof%5C3O8G4r5xgNuaWtirhTmACT2jdv1zQq1vw1SF%2FxOIci9yqdNGgMk1uPahjmbCVlREnHtwc%2Bbe%5C86%3A1744462503374; _iuqxldmzr_=33; playerid=54912904',
}
params = {
    'csrf_token': 'fa390fc86346dc1abaf9b75e428aa241',
}
def get_endata(id):
    with open('原.js','r',encoding='utf-8')as f:
        js = f.read()
    js_code=execjs.compile(js)
    data=js_code.call('get_encrypy_data',id)
    return [data['encText'],data['encSecKey']]
def get_music(music):
    en_data=get_endata(music[0])
    data = {
        'params': en_data[0],
        'encSecKey': en_data[1],
    }
    response = requests.post(
        'https://music.163.com/weapi/song/enhance/player/url/v1',
        params=params,
        cookies=cookies,
        headers=headers,
        data=data,
    )
    # print(response.text)
    res_data=response.json()
    music_No_url=res_data['data'][0]['url']
    partten=r'(https?://[^\s]+?\.(mp3|m4a|wav|ogg))'
    music_url=re.search(partten,music_No_url).group()

    # print(music_url)
    res=requests.get(music_url,cookies=cookies,headers=headers)
    if not os.path.exists('音乐'):
        os.makedirs('音乐')
    filename=f"音乐/{music[1]}-{music[2]}.mp3"
    with open(filename, 'wb') as f:
        f.write(res.content)
def getAnddisplay_musicid_list(target):
    with open('原.js','r',encoding='utf-8')as f:
        js = f.read()
    js_code=execjs.compile(js)
    data_=js_code.call('get_encrypy_list',target)
    data = {
        'params': data_['encText'],
        'encSecKey': data_['encSecKey'],
    }
    response = requests.post(
        'https://music.163.com/weapi/cloudsearch/get/web',
        params=params,
        cookies=cookies,
        headers=headers,
        data=data,
    )
    json_data=response.json()
    song_list=json_data['result']['songs']
    Song_list=[]
    print('序号-------------歌曲--------------歌手')
    for index,song in enumerate(song_list):
        songid=song['id']
        songname=song['name']
        if len(song['ar'])>1:
            Singer=[]
            for ar in song['ar']:
                Singer.append(ar['name'])
                singer='/'.join(Singer)
        else:
            singer=song['ar'][0]['name']
        print(f"{index + 1}-------------{songname}--------------{singer}")
        Song_list.append((songid,songname,singer))
    userwant=input('请输入你要的歌曲序号(可多选,用空格分开):')
    userwant=userwant.strip()
    userwant_list=map(int,userwant.split(' '))
    userwan_list=[]
    for i in userwant_list:
        userwan_list.append(Song_list[i-1])
    return userwan_list
def main(target):
    music_list=getAnddisplay_musicid_list(target)
    for music in music_list:
        get_music(music)
if __name__=='__main__':
    target=input('请输入想搜索的内容:')
    main(target)

